class Solution {
public:
    string reverseWords(string s) {
        int begin = 0;
        int end = 0;
        int next = 0;
        while (next < s.size()) {
            begin = 0 + next;
            end = s.find(' ', begin);
            if (end == string::npos)
                end = s.size();
            next = end + 1;
            end -= 1;
            while (begin < end) {
                swap(s[begin], s[end]);
                begin++;
                end--;
            }
        }
        return s;
    }
};